rm(list=ls())
#=====================================================
# Coefficients Plot
#=====================================================

library(foreign)

# set up the working directory in your computer
setwd("")

# =================================================================================
# Figure 2 : Period - Party ID
# =================================================================================

coef.t <- read.csv("year_LPM_fx_daughter.csv")
coef.t1 <- subset(coef.t, o_dv=="d_repscale")
coef.t2 <- subset(coef.t, o_dv=="d_conscale")

election <- subset(coef.t1, select=c(o_year), complete.cases(o_year))

election$result <- c(1,1,1, # Carter 1977,1978,1980
                     -1,-1,-1, # Reagan 1982,1983,1984
                     -1,-1,-1,-1,# Reagan 1985,1986,1987,1988
                     -1,-1,-1, # Bush 1989,1990,1991
                     1,1,1, # Clinton 1993,1994,1996
                     1,1, # Clinton 1998,2000
                     -1,-1, # Bush 2002,2004
                     -1,-1, # Bush 2006,2008
                     1,1 # Obama 2010,2012
)

y.pch <- ifelse(election$result==1,20,21)
vline <- c(1980,1984,1988,1992,1996,2000,2004,2008)+0.5

png("years_daughter.png", height=2000, width=4000, res=500)
layout(matrix(c(1:1), 1, 1, byrow=T))

  d1 <- subset(coef.t1, is.na(o_year)==F)
  d2 <- subset(coef.t2, is.na(o_year)==F)
  
  b0.all <- d1$o_coef;se0.all <- d1$o_se;sig0.all <- ifelse(d1$o_p<0.05,1,ifelse(d1$o_p<0.1,2,0))
  b1.all <- d2$o_coef;se1.all <- d2$o_se;sig1.all <- ifelse(d2$o_p<0.05,1,ifelse(d2$o_p<0.1,2,0))
  
    
  xid0 <- d1$o_year
  xid1 <- d2$o_year

  y0.color <- ifelse(sig0.all>0,"black","black")
  y1.color <- ifelse(sig1.all>0,"darkgray","darkgray")

  y0.lty <- ifelse(sig0.all>0,1,2);  y0.pch <- y.pch
  y1.lty <- ifelse(sig1.all>0,1,2);  y1.pch <- y.pch
  
plot(xid0, b0.all, xlab = "Survey Year", xlim = c(min(d1$o_year),max(d1$o_year)), 
     ylim=c(-1,1), ylab="Coef. with 95% Confidence Iterval",cex=1,pch=y0.pch, pty="s",col = y0.color, xaxt="n")
for (i in 1:length(b0.all)) lines(rep(xid0[i],2),
                                  c(b0.all[i]-1.96*se0.all[i],b0.all[i]+1.96*se0.all[i]),
                                  col=y0.color[i], lty=y0.lty[i])
points(xid1+0.3, b1.all, cex=1,pch=y1.pch, pty="s",col = y1.color, xaxt="n")
for (i in 1:length(b1.all)) lines(rep(xid1[i]+0.3,2),
                                  c(b1.all[i]-1.96*se1.all[i],b1.all[i]+1.96*se1.all[i]),
                                  col=y1.color[i], lty=y1.lty[i])

abline(h=0)
abline(v=vline, lty=3)
axis(1, at=xid0,labels=xid0, col.axis="black", las=2,cex.axis=0.7)

dev.off()


# =================================================================================
# Figure 3 : Country - Political Ideology
# =================================================================================

rm(list=ls())

# Coefficients Plot (for each country)
# Each country : no-control, control, with survey weights
r2 <- read.csv("d_conscale100_fx_daughter_figure3.csv")


graph.p <- function(k){
  
  ordered <- xx[order(-xx[,"q_N"]),]
  coef <- ordered[,"q_coef"]
  se <- ordered[,"q_se"]
  yid <- c(1:length(coef))+k
  
  color <- ifelse(coef>0,"black","black")
  sig <- ifelse(ordered[,"q_p"]<0.1,1,0)
  pch <- ifelse(sig==1,19,21)
  lty <- ifelse(sig==1,1,2)
  
  points(coef,yid,pch=pch,col=color)
  lci <- ordered[,"q_lci"]
  uci <- ordered[,"q_uci"]
  for (i in 1:length(coef)) lines(c(lci[i],uci[i]),rep(yid[i],2),lty=lty[i],col=color[i])
}



png("country_daughter.png", height=4000, width=2000, res=400)
layout(matrix(c(1:1), 1, 1, byrow=T))

xid <- 1:36
coef <- 1:36
plot(coef,xid,pch="", ylim=c(0,36), xlim=c(-40,25),
     xlab="Coef. with 95% CI",yaxt="n",
     ylab="Estimates of each country are ordered by its sample size")
abline(v=0,col="black")

xx <- subset(r2,q_weight=="noweight"&q_control=="nocontrol")
graph.p(0)

xx <- subset(r2,q_weight=="noweight"&q_control=="control")
graph.p(0.2)

xx <- subset(r2,q_weight=="weight"&q_control=="control")
graph.p(0.4)

yid <- c(1:dim(xx)[1])
ordered <- xx[order(-xx[,"q_N"]),]
aname <- ordered$q_country
text(x=-30,y=yid,labels=aname,srt=0,cex=0.8)

dev.off()
